﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;

namespace MiaProject
{
    class Ship: Actor
    {
        public int score;
        public List<Vector3> points;
        int currentPoint;
        private int speedAttack;
        public int speedOriginal;
        
        public Ship():base()
        {
            pos = new Vector3(0, 1, 2);
            dir = new Vector3(0, 0, 1);
            status = true;
            rotation = 1.0f;
            score = 1120;
            points = new List<Vector3>();
            currentPoint = 0;
            points.Add(new Vector3(0, 0, 0));
            

            this.tiros = new List<Tiro>();
            this.timeTiro = 0;
            this.tiroFrequence = 0.5f;
            this.distTargetAtack = 500;
            this.speedAttack = 10;
            
            this.initSteering();
            this.speedOriginal = this.speed;
        }
        
        public void update(GameTime gameTime)
        {
            Player p = Player.getInstance();
            if (!this.status)
            {
                this.updateTiro(gameTime, p.pos);
                return;
            }

            Vector3 u = new Vector3(1, 0, 0);
            Vector3 v = p.pos - this.pos;

            float distPlayer = v.Length();

            if (distPlayer < this.distTargetAtack)
            {
                
                v.Y = 0;
                v.Normalize();

                float dp = Vector3.Dot(u, v); //dot product
                if (dp > 1.0) dp = 1.0f;
                if (dp < -1.0) dp = -1.0f;

                //verificar side 
                //this.rotation = (1.5f+(float)Math.Acos(dp))*this.side(u,v);
                if (this.side(u, v) > 0)
                    this.rotation = -1.6f - (float)Math.Acos(dp);
                else//
                    this.rotation = 4.7f + (float)Math.Acos(dp);

                this.speed = this.speedAttack;
                
            
            }
            else
            {
                v = this.points[this.currentPoint] - this.pos;
                v.Y = 0;
                v.Normalize();

                float dp = Vector3.Dot(u, v); //dot product
                if (dp > 1.0) dp = 1.0f;
                if (dp < -1.0) dp = -1.0f;

                if (this.side(u, v) > 0)
                    this.rotation = -1.6f - (float)Math.Acos(dp);
                else
                    this.rotation = 4.7f + (float)Math.Acos(dp);

                this.speed = this.speedOriginal;
                
            }

            this.updateSteering(gameTime, this.points[this.currentPoint]);

            //volta o vetor nao normalizado
            v = this.points[this.currentPoint] - this.pos;

            //calcula a distancia do ponto
            float dist = v.Length();

            if (dist < 50)
            {
                this.currentPoint++;
                if (this.currentPoint >= this.points.Count)
                    this.currentPoint = 0;
            }

            this.updateTiro(gameTime, p.pos);
            
        }
        public void draw(Matrix view, Matrix projection, BoundingFrustum frustum)
        {
            this.drawActor(view, projection,frustum);
            this.drawTiroScale(view, projection, this.scaleTiro);
        }


    }


}
